package com.rent.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.rent.bean.Brand;
import com.rent.dao.BrandDao;
import com.rent.utils.BaseDao;
import com.rent.utils.DataAccessUtils;

public class BrandDaoImpl extends BaseDao implements BrandDao {
	DataAccessUtils d = new DataAccessUtils();
	@Override
	public List<Brand> seleAll(int grade) {
		String sql = "select DISTINCT brand from car_n where cstatus =1 ";
		if(grade==1) {
			sql+=" and cgrade = 1";
		}
		sql += " group by brand";
		ResultSet rs = executeQuery(sql);
		List<Brand> list = d.parseBrand(rs);
		return list;
	}
	@Override
	public int add(Brand b) {
		String sql = "insert into car_brand values(null,?);";
		return executeUpdate(sql, b.getBrand());
	}
	@Override
	public int update(Brand b) {
		String sql = "update car_brand set brand=? where bid=?;";
		return executeUpdate(sql, b.getBrand(),b.getBid());
	}
	@Override
	public List<Brand> seleByBrand(Brand b) {
		String sql = "select * from car_brand where 1=1";
		if(b!=null) {
			if(b.getBrand()!=null) {
				sql+=" and brand='"+b.getBrand()+"'";
			}
			if(b.getBid()!=0) {
				sql+=" and bid = "+b.getBid();
			}
		}
		ResultSet rs = executeQuery(sql);
		List<Brand> list = d.parseBrand(rs);
		closeAll(rs);
		return list;
	}
	@Override
	public Brand seleById(int bid) {
		String sql = "select * from car_brand where bid=?;";
		ResultSet rs = executeQuery(sql, bid);
		Brand b = null;
		try {
			if(rs.next()) {
				b = new Brand(rs.getInt("bid"),rs.getString("brand"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs);
		}
		return b;
	}
	
}
